package org.hxyjs.service.impl;

import org.hxyjs.dao.SupplierDao;
import org.hxyjs.dto.DTO;
import org.hxyjs.dto.MessageEnum;
import org.hxyjs.entity.Supplier;
import org.hxyjs.page.PageInfo;
import org.hxyjs.service.SupplierService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

@Service("SupplierService")
public class SupplierServiceImpl implements SupplierService {
    @Resource
    SupplierDao supplierDao;
    @Override
    public DTO<Supplier> getAllSuppliers()  throws Exception{
        List<Supplier> suppliers = supplierDao.getAllSuppliers();
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum  = Optional.ofNullable(suppliers).isPresent()?MessageEnum.GETADMINSUCCESS:MessageEnum.GETADMINFAIL;
        return new DTO<Supplier>(messageEnum.getCode(),messageEnum.getMessage(),suppliers);
    }

    @Override
    public DTO<Supplier> deleteSupplierBySuno(String suno)  throws Exception{
        Integer result = supplierDao.deleteSupplierBySuno(suno);
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum  =result> 0?MessageEnum.DELETEADMINSUCCESS:MessageEnum.DELETEADMINFAIL;
        return new DTO<Supplier>(messageEnum.getCode(),messageEnum.getMessage());
    }

    @Override
    public DTO<Supplier> updateSupplierBySuno(Supplier supplier) throws Exception {
        Integer result = supplierDao.updateSupplierBySuno(supplier);
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum  =result> 0?MessageEnum.UPDATEADMINSUCCESS:MessageEnum.UPDATEADMINFAIL;
        return new DTO<Supplier>(messageEnum.getCode(),messageEnum.getMessage());
    }

    @Override
    public DTO<Supplier> addSupplier(Supplier supplier) throws Exception {
        Integer result = supplierDao.addSupplier(supplier);
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum  =result> 0?MessageEnum.ADDADMINSUCCESS:MessageEnum.ADDADMINFAIL;
        return new DTO<Supplier>(messageEnum.getCode(),messageEnum.getMessage());
    }

    /**
     * 学生选课成功 学生成绩加1分，从选课信息中可以获得学生的学号就能查询出学生原来的成绩
     * @param
     * @return
     * @throws Exception
     */
    @Override
    public DTO<Supplier> getSupplierBySuno(String suno)  throws Exception{
        Supplier suppliers = supplierDao.getSupplierBySuno(suno);
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum  = Optional.ofNullable(suppliers).isPresent()?MessageEnum.GETADMINSUCCESS:MessageEnum.GETADMINFAIL;
        return new DTO<Supplier>(messageEnum.getCode(),messageEnum.getMessage(),suppliers);
    }
    @Override
    public DTO<Supplier> getSupplierByPage(PageInfo pageInfo) throws Exception {
        List<Supplier> suppliers = supplierDao.getSupplierByPage(pageInfo);
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum  = Optional.ofNullable(suppliers).isPresent()?MessageEnum.GETADMINSUCCESS:MessageEnum.GETADMINFAIL;
        return new DTO<Supplier>(messageEnum.getCode(),messageEnum.getMessage(),suppliers,pageInfo);
    }

    @Override
    public DTO<Supplier> selectSupplierWithorderAndUser(String suno) throws Exception {
        Supplier suppliers = supplierDao.selectSupplierWithorderAndUser(suno);
        //isPresent表示判断不为空成立则dto为？与：之间的对象
        MessageEnum messageEnum  = Optional.ofNullable(suppliers).isPresent()?MessageEnum.GETADMINSUCCESS:MessageEnum.GETADMINFAIL;
        return new DTO<Supplier>(messageEnum.getCode(),messageEnum.getMessage(),suppliers);
    }


}
